package me.zhangyifeng.blog.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import me.zhangyifeng.blog.mapper.custom.ActivityMapperCustom;
import me.zhangyifeng.blog.model.domain.Activity;
import me.zhangyifeng.blog.model.domain.User;
import me.zhangyifeng.blog.model.domain.UserActivity;
import me.zhangyifeng.blog.model.dto.JsonResult;
import me.zhangyifeng.blog.service.ActivityService;

/**
 * 活动服务实现类
 * 
 * @author zhangyifeng
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapperCustom activityMapperCustom;

    @Override
    public List<Activity> findAllActivities() {
        return activityMapperCustom.findAllActivities();
    }

    @Override
    public Activity findActivityById(Integer activityId) {
        return activityMapperCustom.findActivityById(activityId);
    }

    @Override
    public Activity findActivityWithParticipationInfo(Integer activityId, Integer userId) {
        Activity activity = activityMapperCustom.findActivityById(activityId);
        if (activity != null) {
            // 查询参与人数
            Integer participantCount = activityMapperCustom.countParticipants(activityId);
            activity.setParticipantCount(participantCount);
            
            // 查询当前用户是否参与
            if (userId != null) {
                Integer isParticipating = activityMapperCustom.checkUserParticipation(userId, activityId);
                activity.setIsParticipating(isParticipating > 0);
            } else {
                activity.setIsParticipating(false);
            }
        }
        return activity;
    }

    @Override
    public List<Activity> findAllActivitiesWithParticipationInfo(Integer userId) {
        List<Activity> activities = activityMapperCustom.findAllActivities();
        
        // 为每个活动添加参与信息
        return activities.stream().peek(activity -> {
            // 查询参与人数
            Integer participantCount = activityMapperCustom.countParticipants(activity.getActivityId());
            activity.setParticipantCount(participantCount);
            
            // 查询当前用户是否参与
            if (userId != null) {
                Integer isParticipating = activityMapperCustom.checkUserParticipation(userId, activity.getActivityId());
                activity.setIsParticipating(isParticipating > 0);
            } else {
                activity.setIsParticipating(false);
            }
        }).collect(Collectors.toList());
    }

    @Override
    public List<User> findParticipantsByActivityId(Integer activityId) {
        return activityMapperCustom.findParticipantsByActivityId(activityId);
    }

    @Override
    public JsonResult joinActivity(Integer userId, Integer activityId) {
        try {
            // 检查用户是否已参与该活动
            Integer isParticipating = activityMapperCustom.checkUserParticipation(userId, activityId);
            if (isParticipating > 0) {
                return new JsonResult(false, "您已经参与了该活动");
            }
            
            // 检查活动是否存在
            Activity activity = activityMapperCustom.findActivityById(activityId);
            if (activity == null) {
                return new JsonResult(false, "活动不存在");
            }
            
            // 添加参与记录
            UserActivity userActivity = new UserActivity(userId, activityId);
            activityMapperCustom.insertUserActivity(userActivity);
            
            return new JsonResult(true, "成功参与活动");
        } catch (Exception e) {
            return new JsonResult(false, "参与活动失败: " + e.getMessage());
        }
    }

    @Override
    public JsonResult quitActivity(Integer userId, Integer activityId) {
        try {
            // 检查用户是否已参与该活动
            Integer isParticipating = activityMapperCustom.checkUserParticipation(userId, activityId);
            if (isParticipating == 0) {
                return new JsonResult(false, "您尚未参与该活动");
            }
            
            // 删除参与记录
            activityMapperCustom.deleteUserActivity(userId, activityId);
            
            return new JsonResult(true, "成功退出活动");
        } catch (Exception e) {
            return new JsonResult(false, "退出活动失败: " + e.getMessage());
        }
    }

    @Override
    public List<Activity> findActivitiesByUserId(Integer userId) {
        return activityMapperCustom.findActivitiesByUserId(userId);
    }

    @Override
    public JsonResult createActivity(Activity activity) {
        try {
            // 基本验证
            if (activity.getActivityName() == null || activity.getActivityName().trim().isEmpty()) {
                return new JsonResult(false, "活动名称不能为空");
            }
            
            activityMapperCustom.insertActivity(activity);
            return new JsonResult(true, "创建活动成功");
        } catch (Exception e) {
            return new JsonResult(false, "创建活动失败: " + e.getMessage());
        }
    }

    @Override
    public JsonResult updateActivity(Activity activity) {
        try {
            // 检查活动是否存在
            Activity existingActivity = activityMapperCustom.findActivityById(activity.getActivityId());
            if (existingActivity == null) {
                return new JsonResult(false, "活动不存在");
            }
            
            // 基本验证
            if (activity.getActivityName() == null || activity.getActivityName().trim().isEmpty()) {
                return new JsonResult(false, "活动名称不能为空");
            }
            
            activityMapperCustom.updateActivity(activity);
            return new JsonResult(true, "更新活动成功");
        } catch (Exception e) {
            return new JsonResult(false, "更新活动失败: " + e.getMessage());
        }
    }

    @Override
    public JsonResult deleteActivity(Integer activityId) {
        try {
            // 检查活动是否存在
            Activity existingActivity = activityMapperCustom.findActivityById(activityId);
            if (existingActivity == null) {
                return new JsonResult(false, "活动不存在");
            }
            
            activityMapperCustom.deleteActivity(activityId);
            return new JsonResult(true, "删除活动成功");
        } catch (Exception e) {
            return new JsonResult(false, "删除活动失败: " + e.getMessage());
        }
    }
}
